console.log('bg');
//监听
window.bg=new background();
window.cmd={
    post(info,sender,callback){
        $.post(info.url,info.data,(txt)=>{
            callback(txt);
        });
    },
    get(info,sender,callback){
        $.get(info,(txt)=>{
            callback(txt);
        });
    },
    ajax(info,sender,callback){
        info.success=callback;
        $.ajax(info);
    },
    run(info,sender,callback){
        callback(eval(`(${info})()`));
    },
    getCookieAll(obj,sender,callback){
        chrome.cookies.getAll(obj,callback);
    },
    getCookie(obj,sender,callback){
        chrome.cookies.get(obj,callback);
    },
    setCookie(obj,sender,callback){
        chrome.cookies.set(obj,callback);
    },
    removeCookie(obj,sender,callback){
        chrome.cookies.remove(obj,callback);
    },
    RunTool(obj,sender,callback){
        let ret=bg[obj.name](...obj.par);
        callback && callback(ret);
    }
};
chrome.runtime.onMessage.addListener((info,sender,callback)=>{
    let pd=undefined;
    Object.keys(cmd).forEach((key)=>{
        if(key===info.key){
            try{
                window.cmd[key](info.value,sender,callback);
            }catch (e) {
                callback(e.message);
            }
            pd=true;
        }
    });
    if(!pd){
        callback(undefined);
    }
    return pd;
});
